package com.pjbao.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.pjbao.Service.model.ReportConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pjbao.Service.EWEServices.RetCode;
import com.pjbao.Service.model.G1Model;
import com.pjbao.Service.model.PersonInfoRecord;

/**
 * Created by 邱鹏 on 2016/5/5.
 */
@Service
public class ReportService {
	Logger logger = LoggerFactory.getLogger("api service v1");
	EWEServices EWEServices = new EWEServices();
	@Autowired
	HttpForReportService http4ReportService;
	@Autowired
	ReportConfig reportConfig;

	/**
	 * @param info
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> exportWordByType(PersonInfoRecord info) throws Exception {
		Map<String, Object> exportData = new HashMap<String, Object>();
		String[] selectedInterface = info.getSelectInterface().split("~");
		Map<String, Object> g1Map = http4ReportService.invokeG_X("g1",info);// 确保身份证和姓名一致
		 ObjectMapper mapper = new ObjectMapper();
		G1Model g1 = mapper.readValue(mapper.writeValueAsString(g1Map), G1Model.class);
		exportData.put("nowTime", TypeUtils.date2Str());
		exportData.put("g1", g1Map);
		logger.info(selectedInterface[0]);
		for(int i=0; i<selectedInterface.length; i++){
			if ("一致".equals(g1.getData().getDesc())) {
				logger.info("G1验证通过开始调取其他接口");
				if("g1".equals(selectedInterface[i])){
					
				}else{
					logger.info("进入:"+selectedInterface[i]);
					exportData.put(selectedInterface[i], http4ReportService.invokeG_X(selectedInterface[i],info));
				}
			}
		}
		return exportData;
	}
	
	

	public Map<String, Object> companyExport(PersonInfoRecord info) throws Exception {
		Map<String, Object> exportData = new HashMap<String, Object>();
		String[] selectedInterface = info.getSelectInterface().split("~");
		for (int i = 0; i < selectedInterface.length; i++) {
			if (selectedInterface[i] != null) {
				 if ("ysBusinessCircles".equals(selectedInterface[i].trim())) {
					RetCode code = EWEServices.ysBusinessCircles(reportConfig.getIp(),info.getComName(),
							Integer.parseInt(info.getKeyType()),info.getUserid());
					ObjectMapper mapper = new ObjectMapper();
					Map<String, Object> ysBusinessCirclesMap = mapper.readValue(code.getData(), Map.class);
					exportData.put("ysBusinessCircles", ysBusinessCirclesMap);
				} else if ("companyRiskProfile".equals(selectedInterface[i].trim())) {
					Map<String, Object> companyRiskProfile=new HashMap<String, Object>();
					String[] comtype = info.getIdList().split(":");
					if("falses".equals(comtype[0])){
						processCompanyRiskProfile(info.getComName(),"cpws",companyRiskProfile,info.getUserid());
						processCompanyRiskProfile(info.getComName(),"zxgg",companyRiskProfile,info.getUserid());
						processCompanyRiskProfile(info.getComName(),"sxgg",companyRiskProfile,info.getUserid());
						processCompanyRiskProfile(info.getComName(),"ktgg",companyRiskProfile,info.getUserid());
						processCompanyRiskProfile(info.getComName(),"fygg",companyRiskProfile,info.getUserid());
						processCompanyRiskProfile(info.getComName(),"wdhmd",companyRiskProfile,info.getUserid());
						processCompanyRiskProfile(info.getComName(),"ajlc",companyRiskProfile,info.getUserid());
						exportData.put("companyRiskProfile",companyRiskProfile);
					}else{
						processCompanyRiskProfile(info.getComName(),comtype[0],companyRiskProfile,info.getUserid());
						exportData.put("companyRiskProfile",companyRiskProfile);
					}

				} else if("ccorpIdList".equals(selectedInterface[i].trim())){
					RetCode code = EWEServices.b402Query(reportConfig.getIp(),info.getIdList(),info.getUserid());
					ObjectMapper mapper = new ObjectMapper();
					 String companyType[] = info.getIdList().split(":"); 
					 Map<String, Object> addTier = mapper.readValue(code.getData(), Map.class);
					Map<String, Object> companyRiskProfile = new HashMap<String, Object>();
					companyRiskProfile.put(companyType[0], addTier);
					exportData.put("companyRiskProfile", companyRiskProfile);		
					
				}else {
					exportData.put(selectedInterface[i], http4ReportService.invokeG_X(info,selectedInterface[i]));
				}
			}
		}
		return exportData;
	}

	public void processCompanyRiskProfile(String comName, String thisType, Map<String, Object> exportData,String userid) {

        RetCode code = EWEServices.fhConditionQuery(reportConfig.getIp(),comName, thisType,userid);
		ObjectMapper mapper = new ObjectMapper();
		try {
			Map<String, Object> resultMap = mapper.readValue(code.getData(), Map.class);
			exportData.put(thisType, resultMap);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	//个人,非实名对应
	public Map<String, Object> exportCode(PersonInfoRecord info) throws Exception{
		Map<String, Object> exportData = new HashMap<String, Object>();
		String[] selectedInterface = info.getSelectInterface().split("~");
		for(int i=0; i<selectedInterface.length; i++){
			if("g1".equals(selectedInterface[i])){
				
			}else{
				exportData.put(selectedInterface[i], http4ReportService.invokeG_X(selectedInterface[i],info));	
			}
		}
		return exportData;
	}
}
